//
//  CSGXInventoryManagerBLL.m
//  CarSalesMS_GCX
//
//  Created by Tony on 2016/12/23.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXInventoryManagerBLL.h"

#import "TSaleInfoDAL.h"

#import "TSaleInfoModel.h"
#import "TInventoryModel.h"


@implementation CSGXInventoryManagerBLL

+ (NSString *)getThreeUnitStr:(NSString *)unitStr countStr:(NSString *)countStr {
    NSString *threeStr = @"";
    
    NSArray *unitArr = [unitStr componentsSeparatedByString:@","];
    NSArray *countArr = [countStr componentsSeparatedByString:@","];
    if (unitArr.count == 2) {
        if (countArr.count == 2) {
            threeStr = [NSString stringWithFormat:@"%@%@,,%@%@", countArr[0], unitArr[0], countArr[1], unitArr[1]];
        }else if (countArr.count == 3) {
            threeStr = [NSString stringWithFormat:@"%@%@,,%@%@", countArr[0], unitArr[0], countArr[2], unitArr[1]];
        }
    }else if (unitArr.count == 3) {
        threeStr = [NSString stringWithFormat:@"%@%@,%@%@,%@%@", countArr[0], unitArr[0], countArr[1], unitArr[1], countArr[2], unitArr[2]];
    }
    return threeStr;
}

///将普通的saleList（可能两个单位）的数量和计算出来，返回字符串
+ (NSString *)getSaleCountTodayWithSaleList:(NSArray<TSaleInfoModel *> *)saleList {
    if (saleList == nil || saleList.count == 0) {
        return nil;
    }
    NSString *countStr = @"";
    
    NSMutableArray *countArr = [NSMutableArray arrayWithArray:@[@0, @0, @0]];
    for (int i = 0; i < saleList.count; i++) {
        NSArray *counts = [saleList[i].sCount componentsSeparatedByString:@","];
        
        if (counts.count == 2) {
            countArr[0] = @([countArr[0] intValue] + [counts[0] intValue]);
            countArr[2] = @([countArr[2] intValue] + [counts[1] intValue]);
        }else if (counts.count == 3) {
            
            countArr[0] = @([countArr[0] intValue] + [counts[0] intValue]);
            countArr[1] = @([countArr[1] intValue] + [counts[1] intValue]);
            countArr[2] = @([countArr[2] intValue] + [counts[2] intValue]);
        }
    }
    for (int i = 0; i < countArr.count; i++) {
        countStr = [countStr stringByAppendingFormat:@"%@,", countArr[i]];
    }
    countStr = [countStr subStringFrontOfLastIndex:1];
    return countStr;
}
    
+ (NSString *)getSumCountWithCountList:(NSArray<NSString *> *)list {
    NSString *countStr = @"";
    
    NSMutableArray *countArr = [NSMutableArray arrayWithArray:@[@0, @0, @0]];
    for (int i = 0; i < list.count; i++) {
        NSArray *counts = [list[i] componentsSeparatedByString:@","];
        
        if (counts.count == 2) {
            countArr[0] = @([countArr[0] intValue] + [counts[0] intValue]);
            countArr[2] = @([countArr[2] intValue] + [counts[1] intValue]);
        }else if (counts.count == 3) {
            
            countArr[0] = @([countArr[0] intValue] + [counts[0] intValue]);
            countArr[1] = @([countArr[1] intValue] + [counts[1] intValue]);
            countArr[2] = @([countArr[2] intValue] + [counts[2] intValue]);
        }
    }
    for (int i = 0; i < countArr.count; i++) {
        countStr = [countStr stringByAppendingFormat:@"%@,", countArr[i]];
    }
    countStr = [countStr subStringFrontOfLastIndex:1];
    return countStr;
}

+ (TInventoryModel *)getInventoryModelWithInventoryList:(NSArray<TInventoryModel *> *)list {
    TInventoryModel *model = [[TInventoryModel alloc] init];
//    NSMutableArray<NSString *> *mUnits = [NSMutableArray<NSString *> array];
    NSMutableArray<NSString *> *mCounts = [NSMutableArray<NSString *> array];
    for (int i = 0; i < list.count; i++) {
//        [mUnits addObject:list[i].iUnit];
        [mCounts addObject:list[i].iInventoryCount];
    }
    model.iId = list[0].iId;
    model.iCoNo = list[0].iCoNo;
    model.iName = list[0].iName;
    model.iUnit = list[0].iUnit; //[self getSumCountWithCountList:mUnits.copy];
    model.iInventoryCount = [self getSumCountWithCountList:mCounts];
    model.iCreateUserNo = list[0].iCreateUserNo;
    model.iCreateTime = list[0].iCreateTime;
    model.iIsChecked = list[0].iIsChecked;
    model.iCheckedTime = list[0].iCheckedTime;
    model.iRemark = list[0].iRemark;
    return model;
}


+ (NSString *)getPriceStr:(NSString *)price {
    NSString *priceStr = @"";
    NSArray *priceArr = [price componentsSeparatedByString:@","];
    if (priceArr.count == 2) {
        priceStr = [NSString stringWithFormat:@"%@,,%@", priceArr[0], priceArr[1]];
    } else if (priceArr.count == 3) {
        priceStr = price;
    }
    return priceStr;
}

+ (CGFloat)getTotalMoneyWithPriceStr:(NSString *)priceStr count:(NSString *)countStr {
    CGFloat totalMoney = 0;
    NSArray *countArr = [countStr componentsSeparatedByString:@","];
    NSArray *priceArr = [priceStr componentsSeparatedByString:@","];
    for (int i = 0; i < countArr.count; i++) {
        totalMoney += [priceArr[i] floatValue] * [countArr[i] intValue];
    }
    return totalMoney;
}














#pragma mark - select
+ (NSArray<TSaleInfoModel *> *)loadSaleListWithCommNO:(NSString *)commNo timeDay:(NSString *)timeDay {
    //
    NSString *timeBegin = [NSString stringWithFormat:@"%@ 00:00:00", timeDay];
    NSString *timeEnd = [NSString stringWithFormat:@"%@ 24:00:00", timeDay];
    NSString *sql = [NSString stringWithFormat:@"select * from tSaleInfo where sCoNo='%@' and sCreateTime between '%@' and '%@'; ",
                     commNo,
                     timeBegin,
                     timeEnd
                     ];
    return [TSaleInfoDAL loadListWithSql:sql];
}











@end
